﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Linq;

public class TaskManager : MonoBehaviour 
{
    
    public static TaskManager instance;
    public XElement xele;
    public List<Task> tasks = new List<Task>();

    public delegate void getDelegate();
    public event getDelegate getevent;

    public delegate void checkDelegate(string id, int amount);
    public event checkDelegate checkEvent;

    
    public delegate void rewardDelegate();
    public event rewardDelegate rewardEvent;

    private void Awake()
    {
        if (!instance)
        {
            instance = this;
        }
        else
            Destroy(this);
    }
    private void Start()
    {
        xele = XElement.Load(Application.dataPath + "/TestTask/Task.xml");
    }
    public void CreateTask(string id)
    {
        for (int i = 0; i < tasks.Count; i++)
        {
            if (tasks[i].id==id)
            {
                return;
            }
        }
        Task t = new Task(id);
        tasks.Add(t);
        getevent += t.Get;
        checkEvent += t.Check;
        rewardEvent += t.Reward;
    }
    public void RemoveTask(string id)
    {
        for (int i = 0; i < tasks.Count; i++)
        {
            if (tasks[i].id==id)
            {
                tasks[i].DestroyTask();
            }
        }
    }
    public void Get()
    {
        if (getevent!=null)
        {
            getevent();
        }
    }
    public void Check(string id,int amount)
    {
        if (checkEvent!=null)
        {
            checkEvent(id,amount);
        }
    }
    public void Reward()
    {
        if (rewardEvent!=null)
        {
            rewardEvent();
        }
    }


}
